﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.Forms.Message;

namespace ElectronicWMSA
{
    public partial class RegistSystem : Form
    {
        private string pcCode;
        PublicDataSet bll_CommonPub = new PublicDataSet();
        TSystemRegistBLL bll_SystemRegist = new TSystemRegistBLL();
        TSystemRegistEntity entity_SystemRegist = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        public RegistSystem()
        {
            InitializeComponent();
            this.labeljqm.BackColor = Color.Transparent;
            this.labelzcm.BackColor = Color.Transparent;
            this.labShow.BackColor = Color.Transparent;
        }
        private void RegistSystem_Load(object sender, EventArgs e)
        {
            pcCode = PCInfoClass.getMNum();
            txtPcCode.Text = pcCode;
            txtRegist.Text = "在此输入注册码！";
            labShow.Text = "";
        }

        private void btnRegist_Click(object sender, EventArgs e)
        {
            if (this.txtRegist.Text == "" || this.txtRegist.Text == "在此输入注册码！")
            {
                this.labShow.Text = "*请输入注册码！";
                this.txtRegist.Focus();
                return;
            }
            string str = this.txtRegist.Text.Trim();
            if (this.txtRegist.Text.Length < 40)
            {
                labShow.Text = "输入注册码有误，请重新输入！";
                return;
            }
            if (PCInfoClass.Encryption(pcCode, "*A9527***") != str.Substring(0, str.IndexOf("&")))
            {
                this.labShow.Text = "输入注册码有误，请重新输入！";
                return;
            }
            if (!bll_SystemRegist.Validate_SystemRegist(pcCode))//检测机器码是否存入数据库
            {
                entity_SystemRegist = new TSystemRegistEntity();
                entity_SystemRegist.PcCode = pcCode;
                entity_SystemRegist.RegCode = this.txtRegist.Text.Trim();
                entity_SystemRegist.Mark = "机器码" + pcCode + "在" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 注册成功！";

                int index_1 = str.IndexOf('&') + 1;
                string s_meun = PCInfoClass.Decryption(str.Substring(index_1), "*A9527**");
                string[] s_meuns = s_meun.Split(',');
                if (bll_SystemRegist.Add_SystemRegist(entity_SystemRegist, s_meuns))
                {
                    MessageClass.right("成功！");
                    this.Hide();
                    this.Close();
                }
                else
                {
                   MessageClass.warning("失败！");
                }
            }
            else
            {
                entity_SystemRegist = new TSystemRegistEntity();
                entity_SystemRegist.PcCode = pcCode;
                entity_SystemRegist.RegCode = this.txtRegist.Text.Trim();
                entity_SystemRegist.Mark = "系统在" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "进行了重新注册。";
                if (MessageClass.enquire("系统已经注册过，是否重新注册系统！") == DialogResult.OK)
                {
                    int index_1 = str.IndexOf('&') + 1;
                    string s_meun = PCInfoClass.Decryption(str.Substring(index_1), "*A9527**");
                    string[] s_meuns = s_meun.Split(',');
                    if (bll_SystemRegist.Delete_SystemRegist(pcCode))
                    {
                        if (bll_SystemRegist.Add_SystemRegist(entity_SystemRegist, s_meuns))
                        {
                            MessageClass.right("成功！");
                            this.Hide();
                            this.Close();
                        }
                        else
                        {
                            MessageClass.error("失败！");
                        }
                    }
                }
            }
        }
        private void btnExit2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        } 
    }
}
